package com.software.loginregister.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.software.loginregister.R;
import com.software.loginregister.entity.Restaurant;
import com.software.loginregister.header.CategoryHeader;
import com.software.loginregister.header.HealthyFoodHeader;

import java.util.List;

public class RestaurantAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_HEALTHY_FOOD = 0;
    private static final int TYPE_RESTAURANT = 1;

    private List<Object> items;

    public RestaurantAdapter(List<Object> items) {
        this.items = items;
    }

    @Override
    public int getItemViewType(int position) {
        Object item = items.get(position);
        if (item instanceof HealthyFoodHeader) {
            return TYPE_HEALTHY_FOOD;
        } else {
            return TYPE_RESTAURANT;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        switch (viewType) {
            case TYPE_HEALTHY_FOOD:
                return new HealthyFoodViewHolder(inflater.inflate(R.layout.item_healthy_food, parent, false));
            default:
                return new RestaurantViewHolder(inflater.inflate(R.layout.item_restaurant, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Object item = items.get(position);
        if (holder instanceof HealthyFoodViewHolder) {
            ((HealthyFoodViewHolder) holder).bind((HealthyFoodHeader) item);
        } else if (holder instanceof RestaurantViewHolder) {
            ((RestaurantViewHolder) holder).bind((Restaurant) item);
        }
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    static class HealthyFoodViewHolder extends RecyclerView.ViewHolder {
        private TextView tvTitle;
        private LinearLayout healthyFoodContainer;

        public HealthyFoodViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_healthy_title);
            healthyFoodContainer = itemView.findViewById(R.id.healthy_food_container);
        }

        public void bind(HealthyFoodHeader header) {
            healthyFoodContainer.removeAllViews();

            for (String food : header.getHealthyFoodList()) {
                TextView foodItem = new TextView(itemView.getContext());
                foodItem.setText(food);
                foodItem.setTextSize(14);
                foodItem.setTextColor(Color.parseColor("#666666"));
                foodItem.setPadding(0, 8, 0, 8);
                healthyFoodContainer.addView(foodItem);
            }
        }
    }

    static class RestaurantViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivRestaurant;
        private TextView tvName;
        private TextView tvRating;
        private TextView tvSales;
        private TextView tvTag1;
        private TextView tvTag2;

        public RestaurantViewHolder(@NonNull View itemView) {
            super(itemView);
            ivRestaurant = itemView.findViewById(R.id.iv_restaurant);
            tvName = itemView.findViewById(R.id.tv_restaurant_name);
            tvRating = itemView.findViewById(R.id.tv_rating);
            tvSales = itemView.findViewById(R.id.tv_sales);
            tvTag1 = itemView.findViewById(R.id.tv_tag1);
            tvTag2 = itemView.findViewById(R.id.tv_tag2);
        }

        public void bind(Restaurant restaurant) {
            ivRestaurant.setImageResource(restaurant.getImageResource());
            tvName.setText(restaurant.getName());
            tvRating.setText(restaurant.getRating());
            tvSales.setText(restaurant.getMonthSales());
            tvTag1.setText("精选健康餐厅");
            tvTag2.setText("平台保障");
        }
    }
}